﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
///WmlData 的摘要说明
/// </summary>
public class WmlData
{
	public WmlData()
	{

	}

    public static string buildJsonTWml(string domainSuf,string directory)//将json数据转换为wml数据供手机使用.
    {
        string filename = HttpContext.Current.Server.MapPath("") + "//userdata//" + domainSuf+"//"+directory + "//sendData.json";
        if (!File.Exists(filename))
        {
            domainSuf = "www";
        }
        StringBuilder htmlTable = new StringBuilder();
        Xmlserdeser xser = new Xmlserdeser(HttpContext.Current.Server.MapPath("") + "//userdata//", domainSuf,directory);
        page myPage = xser.Mpage;
        //htmlTable.Append("<p>账号:<inputname='accountno' type='text' maxlength='12'/>密码:<inputname='accountpin' type='password' maxlength='4'/><dotype='accept'label='CheckBal'><go href='login.aspx?flag=wap' method='post'><postfieldname='accountno'value='$Accountno'/><postfieldname='accountpin'value='$Accountpin'/></go></do></p>");

        htmlTable.Append("<table>");
        for (int i = 0; i < myPage.Mtables.Count; i++)
        {
            htmlTable.Append("<tr><td align='l'><table>");
            table tmpTable = myPage.Mtables[i];
            if (null == tmpTable)
                continue;
            for (int j = 0; j < tmpTable.Mrows.Count; j++)
            {
                htmlTable.Append("<tr><td>");
                row tmpRow = tmpTable.Mrows[j];
                if (tmpRow.Ct.IndexOf("[") > -1)
                {
                    htmlTable.Append("<a >" + tmpRow.Ct + "</a>");
                }
                else
                {
                    htmlTable.Append("[<a >" + tmpRow.Ct + "</a>]");
                }
                for (int k = 0; k < tmpRow.Mrowwebs.Count; k++)
                {
                    web tmpWeb = tmpRow.Mrowwebs[k];
                    if (null == tmpWeb)
                    {
                        continue;
                    }
                    if (!Regex.IsMatch(tmpWeb.Link, "(http|https|ftp|mms|rtsp|gopher|socks4|socks5)://.*"))
                    {
                        tmpWeb.Link = "http://" + tmpWeb.Link;
                    }
                    htmlTable.Append("<a href='" + tmpWeb.Link + "'>" + tmpWeb.Name + "</a>");
                }
                htmlTable.Append("</td></tr>");
            }
            htmlTable.Append("</table></td><td></td></tr><tr><td><td><td></td></td></td></td></tr>");
        }
        htmlTable.Append("</table>");

        return htmlTable.ToString();
    }
}